<?php 
/**
 * @desc:简单的应用操作基类
 * app({
 * 		needAct=>array(可执行的方法名)
 * 		items=>array(
 * 			name,gpc_name,gpc_type,tpl_name,type,must=>array(act=>true/false),error_msg=>array(act=>msg_text)
 * 		)
 * })
 * @author: Jack Brown
 * @version: 1.0
 * @time: 2012-11-23
 * 
 */
	if(!defined('IN_WEB')){
		exit('Aceess Deny!');
	}

	
	class app{
		
		/*指定应用所需的参数*/
		protected  $items = array();
		
		/*应用名称*/
		protected  $appName = 'app-base';
		
		/*所需行为*/
		protected $needAct = array();
		
		/*应用中所需的变量*/
		protected $appVars = array();
		
		
		/*需要操作表，可选*/
		protected $appTable = array();
		
		/*应用默认执行的操作*/
		protected $default;
		
		/*是否可执行对应的操作*/
		protected $canExcute = true;
		
		/**是否是api模式**/
		protected $apiMode = false;
		
		/**for debug 记录不能执行的字段名称**/
		protected $exceptItem = array();
		
		public function __construct($params){
						
			if($params['needAct']==null){
				$this->error('请指定应用操作！');
				return false;
			}
			
			foreach($params['needAct'] as $key=>$act){
				if(!method_exists($this,$act)){
					$this->error('不存在的应用操作！');
					return false;
				}
			}	
			

			$this->needAct = $params['needAct'];			
			$this->items = $params['items'];		
			$this->appVars = $GLOBALS;
			$this->template = $params['template'];
			$this->appVars['ui']->assign('cur_app',get_class($this));
			$this->appTable = $params['app_table'];
			$this->default = $params['def_act'];
			$this->appName = get_class($this);
			
			if(defined('API_MODE')){
				$this->apiMode = true;
			}
		}
		
		/**
		 * 执行应用的操作行为
		 */
		public function run(){
			
			if($this->needAct == null){
				return false;
			}
			
			if(in_array($this->appVars['_REQUEST']['action'],$this->needAct)){
				$action = $this->appVars['_REQUEST']['action'];
				$this->exploreItem($action);
				$data = call_user_method($action,$this);
			}else if(in_array($this->default,$this->needAct)){
				$action = $this->default;
				$this->exploreItem($action);
				$data = call_user_method($action,$this);			
			}else{
				return false;
			}
			
			$this->appVars['ui']->assign('act',$action);
			if($this->template!=null){
				$this->appVars['ui']->display($this->template);
			}
			
			return $data;
		}
		
		/**
		 * 执行添加的操作
		 * */
		protected function add(){
			
			if($this->appTable!=null){
				$addObj = new infor($this->appTable['name'],$this->appTable['key']);
				$this->appVars['addObj'] = $addObj;
				
				if($this->canExcute){
					return $addObj->add($this->appVars['add']);
				}else{
					return false;
				}
			}
			
			return false;
		}
		
		/**
		 * 执行更新的操作
		 * */
		protected function update(){
			
			if($this->appTable!=null){
			
				$updateObj  = $this->buildObject();
				$this->appVars['updateObj'] = $updateObj;
				if($this->canExcute){
					return $updateObj->set($this->appVars['update']);
				}else{			
					return false;
				}
				
			}
			
			return false;
		}
		
		/**
		 * 执行删除的操作
		 */
		protected function delete(){

			if($this->appTable!=null){
			
				/*此处是决定在删除之前要做什么*/
				if($this->appVars['delObj']==null){
					$delObj = new infor($this->appTable['name'],$this->appTable['key'],$this->appTable['val']);
					$this->appVars['delObj'] = $delObj;
				}else{
				
					$delObj = $this->appVars['delObj'];
					/**批量删除时需要重定义key的值**/
					$delObj->setKeyVal($this->appTable['val']);
				}
				
				if($this->canExcute){
					return $delObj->remove();
				}else{
					return false;
				}
			}
			
			return false;
		}
		
		/**
		 * 执行显示操作
		 */
		protected function showList(){
		
			
		}
		
		
		/**
		 * 遍历变量
		 * @param $actName 执行操作的名称
		 * 
		 */
		protected function exploreItem($actName){
			
			if(!method_exists($this,$actName)){
				return false;
			}
			
			if($this->items == null){
				return false;
			}
			
			if($this->appVars[$actName]!=null){
				return false;
			}
		
			$this->appVars[$actName]= array();
			foreach($this->items as $item){
				if(isset($item['name'])){
				
					if($item['conv_type']!='' && function_exists($item['conv_type'])){
						$convFunc = $item['conv_type'];
					}
					
					if($item['name']!='' && $item['type']!=''){
						$temp = '';
						$type = $item['type'];
						if($item['fixed_value']!=''){
						
							$temp = $item['fixed_value'];
							
						}else if($item['gpc_name']!=''){
						
							if($item['gpc_type']=='get'){
								$temp = $this->appVars['_GET'][$item['gpc_name']];
							}else if($item['gpc_type']=='post'){
								$temp = $this->appVars['_POST'][$item['gpc_name']];
							}else if($item['gpc_type']=='cookie'){
								$temp = $this->appVars['_COOKIE'][$item['gpc_name']];
							}else if($item['gpc_type']=='session'){
								$temp = $this->appVars['_SESSION'][$item['gpc_name']];
							}else if($item['gpc_type']=='request'){
								$temp = $this->appVars['_REQUEST'][$item['gpc_name']];
							}
						}
						

						if($item['required'][$actName] == true){
							if(validate::isNotEmpty($temp)){
								if(method_exists(validate,$type)){	
									if(is_array($temp)){
									
										if($item['allow_array']==true){
											foreach($temp as $k=>$var){					
												if(!validate::$type($var)){
													$this->showAppMsg(defVal($item['error_msg'][$actName],'数据格式不正确！'));										
												}
												if($convFunc!=''){
													$temp[$k] = $convFunc($var);
												}
											}
											$this->appVars[$actName][$item['name']] = $temp;
										}else{
											$this->showAppMsg(defVal($item['error_msg'][$actName],'数据格式不正确！'));
										}
										
									}else{
									
										if(!validate::$type($temp)){
											$this->showAppMsg(defVal($item['error_msg'][$actName],'数据格式不正确！'));										
										}else{
										
											if($convFunc!=''){
												$temp = $convFunc($temp);
											}
										
											$this->appVars[$actName][$item['name']] = $temp;
										}
									
									}	
									
								}else{
									$this->error('数据类型不正确!');
								}
							}else{
								$this->exceptItem[] = $item['name'];
								$this->canExcute = false;
							}
							
						}else{
						
							if(validate::isNotEmpty($temp)){
								if(method_exists(validate,$type)){
									if(is_array($temp)){
										if($item['allow_array']==true){
											foreach($temp as $k=>$var){					
												if(!validate::$type($var)){
													$this->showAppMsg($item['error_msg'][$actName]);										
												}
												
												if($convFunc!=''){
													$temp[$k] = $convFunc($var);
												}
												
											}
											$this->appVars[$actName][$item['name']] = $temp;
										}else{
											$this->showAppMsg(defVal($item['error_msg'][$actName],'数据格式不正确！'));
										}
									}else{
										if(!validate::$type($temp)){
											$this->showAppMsg(defVal($item['error_msg'][$actName],'数据格式不正确！'));										
										}else{
										
											if($convFunc!=''){
												$temp = $convFunc($temp);
											}
										
											$this->appVars[$actName][$item['name']] = $temp;
										}
									
									}	
																	
								}else{
									$this->error('数据类型不正确!');
								}
							}						
							
						}
						
						
						if($temp!='' && $item['tpl_name']!=''){
							$this->appVars['ui']->assign($item['tpl_name'],$temp);
						}
						
					}
				}
			}		
		}
		
		/**
		 * 返回当前设置的模型表对象
		 */
		protected function buildObject(){
			if($this->appTable==null){
				return false;
			}
			
			return new infor($this->appTable['name'],$this->appTable['key'],$this->appTable['val']);
		}
		
		
		
		
		/**
		 * 显示提示信息
		 * @param string $msg 
		 */
		protected function showAppMsg($msg,$error=true){
			
			if(!$this->apiMode){
				if($error){
					exit('
					<script type="text/javascript">
	
						inv_resp_error = true;
						showAlert("login_alert","'.$msg.'");
					</script>'
					);				
				}else{
					exit('
					<script type="text/javascript">
						showAlert("login_alert","'.$msg.'");
					</script>'
					);
				}
			}else{
				if($error){
					die( serialize(array('error'=>array('code'=>'5008','text'=>$msg))) );
				}else{
					die( serialize(array('ok'=>'1')) );
				}
			}
		
		}
		
		/**
		异常报告
		*/
		protected function error($exception){	
			
			if(class_exists('error') && is_object($GLOBALS['error'])){			
				$GLOBALS['error']->showErrorStr(get_class($this).';'.$exception,'javascript:history.back()',false);
			}else{
				throw new Exception(get_class($this).';'.$exception);
			}
		}
	}

?>